package com.cn.lyl.lawyer.service.impl;

import com.cn.lyl.lawyer.dao.NewsMapper;
import com.cn.lyl.lawyer.dao.NewsTypeDependenceMapper;
import com.cn.lyl.lawyer.dao.NewsTypeMapper;
import com.cn.lyl.lawyer.model.entity.NewsModel;
import com.cn.lyl.lawyer.model.entity.NewsTypeDependenceModel;
import com.cn.lyl.lawyer.model.entity.NewsTypeModel;
import com.cn.lyl.lawyer.model.vo.NewsTypeVo;
import com.cn.lyl.lawyer.service.NewsService;
import com.cn.lyl.lawyer.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

@Service
public class NewServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsTypeMapper newsTypeMapper;
    @Autowired
    private NewsTypeDependenceMapper newsTypeDependenceMapper;
    @Override
    public List<NewsTypeVo> findAll(NewsTypeVo newsTypeVo) {
        return newsMapper.findAll(newsTypeVo);
    }

    @Override
    public int getCount(NewsTypeVo newsTypeVo) {
        return newsMapper.countFindAll(newsTypeVo);
    }

    @Override
    @Transactional
    public void add(NewsTypeVo newsTypeVo) {
        NewsModel newsModel = new NewsModel();
        newsModel.setAuthor(Tool.stringIsNotNull(newsTypeVo.getAuthor()));
        newsModel.setContent(Tool.stringIsNotNull(newsTypeVo.getContent()));
        newsModel.setTitle(Tool.stringIsNotNull(newsTypeVo.getTitle()));
        newsModel.setFace(Tool.stringIsNotNull(newsTypeVo.getFace()));
        newsMapper.add(newsModel);
        NewsTypeModel byType = newsTypeMapper.findByType(newsTypeVo.getType());
        NewsTypeDependenceModel newsTypeDependenceModel = new NewsTypeDependenceModel();
        newsTypeDependenceModel.setNewsId(newsModel.getId());
        newsTypeDependenceModel.setTypeId(byType.getId());
        newsTypeDependenceMapper.insert(newsTypeDependenceModel);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        newsMapper.delete(id);
        newsTypeDependenceMapper.deleteByNewsId(id);
    }

    @Override
    public NewsTypeVo findById(Integer id) {
        return newsMapper.findById(id);
    }

    @Override
    public void change(NewsTypeVo newsTypeVo) {
        NewsModel newsModel = new NewsModel();
        newsModel.setAuthor(Tool.stringIsNotNull(newsTypeVo.getAuthor()));
        newsModel.setContent(Tool.stringIsNotNull(newsTypeVo.getContent()));
        newsModel.setTitle(Tool.stringIsNotNull(newsTypeVo.getTitle()));
        newsModel.setFace(Tool.stringIsNotNull(newsTypeVo.getFace()));
        newsModel.setId(newsTypeVo.getId());
        newsMapper.update(newsModel);
        NewsTypeModel byType = newsTypeMapper.findByType(newsTypeVo.getType());
        NewsTypeDependenceModel newsTypeDependenceModel = new NewsTypeDependenceModel();
        newsTypeDependenceModel.setNewsId(newsModel.getId());
        newsTypeDependenceModel.setTypeId(byType.getId());
        newsTypeDependenceMapper.update(newsTypeDependenceModel);
    }

    @Override
    public void changeHomeState(Integer id) {
        newsMapper.updateIsHome(id);
    }

    @Override
    public List<NewsTypeModel> findAllType() {
        return newsTypeMapper.findAll();
    }
}
